<template>
  <div class="index-div">
    <el-form class="login-form" :rules="rules" ref="user" :model="user" >
      <div  class="logoImg"></div>
    <el-form-item prop="username">
      <el-input v-model="user.preUsername"></el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input v-model="user.password"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button class="login-btn" type="primary" @click="valite('user')" :loading="loginLoading">登录</el-button>
    </el-form-item>
    </el-form>
  </div>
</template>

<script>

import req from '@/utils/request.js'
import qs from 'qs'
export default {
  name: 'loginIndex',
  components: {},
  props: {},
  data () {
    return {
      user: {
        username: '',
        password: '',
        preUsername: ''
      },
      rules: {
        preUsername: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      },
      loginLoading: false
    }
  },
  methods: {
    onSubmit () {
      this.user.username = 'PC:' + this.user.preUsername
      console.log(this.user)
      // 开启遮罩
      this.loginLoading = !this.loginLoading
      req({
        method: 'POST',
        url: '/api-uaa/oauth/user/token',
        headers: {
          Authorization: 'Basic YXBwOmFwcA==',
          loginType: 'app'
        },
        data: qs.stringify(this.user)
      }).then(res => {
        // 关闭遮罩
        this.loginLoading = !this.loginLoading
        console.log(res.data.datas.access_token)
        localStorage.setItem('token', 'Bearer ' + res.data.datas.access_token)
        console.log(localStorage.getItem('token'))
        // 登录跳转页面 方式一
        // this.$router.push({
        //   name: 'home'
        // })
        // 页面跳转方式 二
        this.$router.push('/home')
      })
    },
    valite (user) {
      this.$refs[user].validate((valid) => {
        if (valid) {
          this.onSubmit()
        } else {
          console.log('error valid!!')
          return false
        }
      })
    }
  }
}
</script>
<style scoped lang='less'>
.index-div {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0; // 全屏
  display: flex;
  justify-content: center;
  align-items: center;
  background: url('../../assets/imgs/loginImg.jpg') no-repeat;
  background-size: cover;
  .login-form {
    background-color: white;
    padding: 30px;
    min-width: 220px;
    min-height: 160px;
  }
  .login-btn {
    width: 100%;
  }
  .logoImg {
    margin-top: -14px;
    margin-bottom: 13px;
    width: 100%;
    height: 30px;
    background: url('../../assets/imgs/logo.png') no-repeat;
    background-size:100% 100%;
    // background-size: cover;
  }
}
</style>
